﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using MSIACCSL2.Views.Inventory;
using WcfService1.DTO;


namespace MSIACCSL2.Views.PreSalesOrder
{
    public partial class FrmAdd : Page
    {
        private int i;
        private List<TextBox> txtNoLog;
        private InventorySearch si;

        public PreSalesOrderViewModel VM
        {
            get { return (PreSalesOrderViewModel) this.DataContext; }
        }

        public PreSalesOrderWrapViewModel WMW
        {
            get { return (this.Resources["POW"] as PreSalesOrderWrapViewModel);  }
        }

        public FrmAdd()
        {
            InitializeComponent();
            txtNoLog = new List<TextBox>();
          

            this.Loaded += (x, y) =>
                               {
                                   (this.Resources["POW"] as PreSalesOrderWrapViewModel).PreSalesOrderViewModel =
                                        (PreSalesOrderViewModel)this.DataContext;
                                   
                               };
        

         

            txtDesc.KeyDown += (o, e) =>
                                   {
                                       if (e.Key == Key.Enter)
                                                txtSerialNumber.Focus();
                                   };
          

        
            this.KeyDown += (o, e) =>
                              {
                                  if (e.Key == Key.A && Keyboard.Modifiers == ModifierKeys.Control )
                                      WMW.AddPreSalesOrderDetail(null);
                                  else if (e.Key == Key.D && Keyboard.Modifiers == ModifierKeys.Control)
                                      WMW.DeletePreSalesOrderDetail(null);
                                  else if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                                      VM.SavePreSalesOrder(VM.PreSalesOrder);
                                  else if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                                      VM.Cancel(null);
                              };

            txtSerialNumber.KeyDown += (o, e) =>
                                           {
                                               if (e.Key == Key.Enter)
                                               {
                                                   WMW.GetInventory2(txtSerialNumber.Text);
                                                   txtSerialNumber.Text = string.Empty;
                                               }
                                           };
        }

      

        private void OkaTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                WMW.GetInventory(((TextBox)sender).DataContext);
            }
                
        }

        private void OkaTextBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
        }

      
        private void btnFind_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            si = new InventorySearch();
            si.Closed += new EventHandler(si_Closed);
            si.Show();
        }

        void si_Closed(object sender, EventArgs e)
        {
            var inv = (sender as InventorySearch).Inventory;
            if (inv != null) txtSerialNumber.Text = inv.SerialNumber;
            txtSerialNumber.Focus();
        }
 

        private void OkaTextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        //Lookup Inventory
        private void btnfind_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            InventorySearch inventorySearch = new InventorySearch();
            inventorySearch.Closed += new EventHandler(inventorySearch_Closed);
            inventorySearch.Show();
        }


        //Search Inventory Finished
        void inventorySearch_Closed(object sender, EventArgs e)
        {
            var inv = (sender as InventorySearch).Inventory;
            var SoDetail = Dg1.SelectedItem as PreSalesOrderDetailDTO;
            SoDetail.Inventory = inv;
            SoDetail.Inventory = inv;
            SoDetail.Panjang = inv.Panjang;
            SoDetail.Lebar = inv.Lebar;
            SoDetail.Tinggi = inv.Warehouses.FirstOrDefault(x => x.KodeWarehouse == VM.PreSalesOrder.Warehouse.KodeWarehouse).Sheet;
            SoDetail.QtyUnit = inv.Warehouses.FirstOrDefault(x => x.KodeWarehouse == VM.PreSalesOrder.Warehouse.KodeWarehouse).Qty;

            Dg1.Focus();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	VM.LoadDetailFromFile(null);
        }

      
        private void TextBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            //PreSalesOrderDetailDTO preSalesOrderDetail = (sender as TextBox).DataContext as PreSalesOrderDetailDTO;
            //if (preSalesOrderDetail != null)
            //    if (preSalesOrderDetail.Inventory.Item.IsRounded)
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", preSalesOrderDetail.QtyUnit);
            //    }
            //    else
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:0.0000}", preSalesOrderDetail.QtyUnit);
            //    }
        }


    }
}

